package cloud.xiangyixiang.www.spring.boot.java.basics.classes;

import java.util.Optional;

/**
 * <p>
 * StudentOptional
 * </p>
 *
 * @author weixin
 * @date 2023/9/18 19:18
 */
public class StudentOptional {

    private String name = "list";

    public String getName() {
        return name;
    }

    public static void getStudentName(SchoolOptional school) {
        TeacherOptional teacher;
        if (school != null) {
            teacher = school.getTeacher();
        } else {
            teacher = new TeacherOptional();
        }

        StudentOptional student;
        if (teacher != null) {
            student = teacher.getStudent();
        } else {
            student = new StudentOptional();
        }

        String name = "";
        if (student != null) {
            name = student.getName();
        } else {
            name = "ZhangSan";
        }
        System.out.println("java7及以下：" + name);
    }

    public static void getStudentNameByOptional(SchoolOptional school){
        Optional<SchoolOptional> optional = Optional.ofNullable(school);
        String name = optional.map(SchoolOptional::getTeacher).map(TeacherOptional::getStudent).map(StudentOptional::getName).orElse("LiSi");
        System.out.println("Java8 Optional: " + name);
    }

    public static void main(String[] args) {
        getStudentName(null);
        getStudentNameByOptional(null);
    }

}
